/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.property.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.property.domain.Payment;
import me.zhengjie.modules.property.domain.Repay;
import me.zhengjie.modules.property.service.LoanCalculator;
import me.zhengjie.modules.property.service.RepayService;
import me.zhengjie.modules.property.service.dto.RepayQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author yyj
* @date 2023-04-21
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "repay管理")
@RequestMapping("/api/repay")
public class RepayController {

    private final RepayService repayService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('repay:list')")
    public void download(HttpServletResponse response, RepayQueryCriteria criteria) throws IOException {
        repayService.download(repayService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询repay")
    @ApiOperation("查询repay")
    @PreAuthorize("@el.check('repay:list')")
    public ResponseEntity<Object> query(RepayQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(repayService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增repay")
    @ApiOperation("新增repay")
    @PreAuthorize("@el.check('repay:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Repay resources){
        return new ResponseEntity<>(repayService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改repay")
    @ApiOperation("修改repay")
    @PreAuthorize("@el.check('repay:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Repay resources){
        repayService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除repay")
    @ApiOperation("删除repay")
    @PreAuthorize("@el.check('repay:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        repayService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出信息模板")
    @ApiOperation("导出信息模板")
    @PreAuthorize("@el.check('property:list')")
    @GetMapping(value = "/export")
    public void exportTemplate(HttpServletResponse response, RepayQueryCriteria criteria) throws IOException {
        repayService.exportTemplate(response);
    }


    @Log("批量导入信息")
    @ApiOperation("批量导入信息模板")
    @PreAuthorize("@el.check('property:import')")
    @PostMapping(value = "/upload")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) throws Exception {
        return repayService.upload(file);
    }



    @GetMapping(value="/countTotalNum")
    @Log("查询repay")
    @ApiOperation("查询repay")
    @PreAuthorize("@el.check('repay:list')")
    public ResponseEntity<Object> countTotalNum(){
        return new ResponseEntity<>(repayService.countTotalNum(),HttpStatus.OK);
    }

    @GetMapping("/equalPrincipalAndInterest")
    @AnonymousAccess
    public List<Payment> calculateEqualPrincipalAndInterest(
            @RequestParam("loanAmount") BigDecimal loanAmount,
            @RequestParam("annualInterestRate") BigDecimal annualInterestRate,
            @RequestParam("loanTermMonths") int loanTermMonths) {
        System.out.println( LoanCalculator.calculateEqualPrincipalAndInterest(loanAmount, annualInterestRate, loanTermMonths));
        return LoanCalculator.calculateEqualPrincipalAndInterest(loanAmount, annualInterestRate, loanTermMonths);
    }

    @AnonymousAccess
    @GetMapping("/equalPrincipal")
    public List<Payment> calculateEqualPrincipal(
            @RequestParam("loanAmount") BigDecimal loanAmount,
            @RequestParam("annualInterestRate") BigDecimal annualInterestRate,
            @RequestParam("loanTermMonths") int loanTermMonths) {
        return LoanCalculator.calculateEqualPrincipal(loanAmount, annualInterestRate, loanTermMonths);
    }
}